<template>
	<view class="container">
		<view class="search-body" >
			<text class="title">当前学生：</text>
			<view class="search-desc" v-if="activeStudent">
				<span>{{activeStudent.studentName}}</span>
				<span>{{activeStudent.idCard}}</span>
			</view>
			<view class="search-desc" v-else>
				<span>暂无绑定</span>
			</view>
			<text class="title">其他学生：</text>
			<view class="search-desc desc-other" @click="switchBind(item)"  v-for="(item, index) in otherStudent" :key="index">
				<span>{{item.studentName}}</span>
				<span>{{item.idCard}}</span>
			</view>
			<button type="primary" class="bind-button" @click="clickBind">添加绑定</button>
		</view>
				<show-modal></show-modal>
	</view>
	
</template>

<script>
		import { queryStudentByCard  } from '@/api/system/system'
		import {queryStudent ,bindStudent} from '@/api/member/member'
	export default {
		data() {
			return {
				idcard:undefined,
				descShow:false,
				student:{},
				bindFrom:{},
				memberId:uni.getStorageSync('userId'),
				activeStudent:undefined,
				otherStudent:[]
			}
		},
		created() {
		  this.getStudentinfo()
		},
		methods: {
			getStudentinfo(){
				const memberId = this.memberId;
				queryStudent(memberId).then(res =>{
					this.otherStudent = []
					for (let i = 0; i < res.data.length; i++) {
						if(res.data[i].defaultStatus == 1){
							this.activeStudent = res.data[i]
						}else{
							this.otherStudent.push(res.data[i])
						}
					}
				}).catch(() =>{
					
				})
			},
			switchBind(item){
				let that = this
				this.$showModal({
					title: '',
					icon:'/static/images/warning.png',
					content: '是否将该学生设为默认学生？',
					cancelText:"暂不设置",
					confirmText:"设置",
					message:false,
					success(res) {
						let status
						if (res.confirm) {
							status = 1
							that.bindStudent(status,item.id)
						} 
						else {
							status = 0
						}
							
					}
				})
			},
			clickBind(){
				this.$tab.navigateTo('/pages/mine/student/bindStudent')				
			},
			bindStudent(status,studentId){
				
				this.bindFrom ={
					memberId:uni.getStorageSync('userId'),
					status,
					studentId,
					type:1
				}
				bindStudent(this.bindFrom).then(res =>{
					uni.setStorageSync('studentId', studentId)
					uni.showToast({
						title: '提交成功',
						duration: 2000
					});
					this.getStudentinfo()
				}).catch(() =>{
					this.$modal.msgError("绑定失败")
				})
			}
		}
	}
</script>

<style lang="scss">
	.container{
		width: calc(100vw - 60rpx);
		margin: 80rpx auto;
		border-radius: 20rpx;
	}

	.search-body{
		width: 100%;
		margin-top: 100rpx;
		background: #FFFFFF;
		box-shadow: 0px 2px 34px 5px rgba(156,193,220,0.25);
		border-radius: 10rpx;
		padding: 40rpx 0;
		.title{
			display: block;
			font-size: 14px;
			font-weight: bold;
			color: #000000;
			padding-left: 40rpx;
		}
		.search-desc{
			width: 80%;
			height: 100rpx;
			color: #fff;
			margin: 60rpx auto;
			background: #FF9A50;
			box-shadow: 0px 2px 34px 5px rgba(156,193,220,0.25);
			border-radius: 20rpx;
			opacity: 1;
			span{
				line-height: 100rpx;
				padding-left: 50rpx;
			}
		}
		.desc-other{
			background: rgba(255,154,80,0.5);
		}
		.bind-button{
			margin-top: 80rpx;
			width: 282px;
			height: 50px;
			background: rgba(255,154,80,0.5);
			box-shadow: 0px 2px 34px 5px rgba(156,193,220,0.25);
			border-radius: 20rpx;
			opacity: 1;
		}
	}
</style>